﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca Kamchatka - copyright 2007(c) Ruben Sainz de la Maza
 
	@class: 	Country
	@vertion: 	0.1
	@date:		28/11/07
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
	@usage: 
		var objCountry:Country = new Country(Object);
		_objCountry[pObject] = objCountry;	
		
**/

package com.conelmate.cak.board.countries
{
	
	import com.conelmate.util.Trace;
	import com.conelmate.cak.events.CountryEvent;

	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.TextFormat;
	
	public class Country extends Trace implements ICountry
	{
		
		private 		var _objCSM			:CountryStateMachine;	//ref a la maquina de estados
		
		private static	var _PARENT			:DisplayObject;			//ref al root
		
		private 		var	_isFreeze		:Boolean = false;		//true:congela eventos, false:reactiva eventos
		
		private			var	_objData		:Object;				//objeto contenedor de datos del pais
		
		private 		var _MC				//:Sprite;
		

//_________________________________________________________________________________ Constructor
		public function Country(pData:Object = null)
		{
			_isTrace = false;
			mTrace("#Country# constructor");
			
			if(pData 	!= null) _objData  = pData;
			
			
			//verificar:
		/*	mTrace("id:"+_objData.id);
			mTrace("limits:"+_objData.limits);
			mTrace("name:"+_objData.name);
			mTrace("continent id:"+_objData.continent.id);
			mTrace("continent name:"+_objData.continent.name);
			mTrace("continent qty:"+_objData.continent.qty);
			mTrace("icon card id:"+_objData.icon.id);
			mTrace("icon card icon:"+_objData.icon.name);
			mTrace("player color:"+_objData.player);
			
			mTrace("");
		*/
		
			_objCSM = new CountryStateMachine();
			_objCSM.init();
		
			//Setear el estado del pais: ATTACK DEFEND PUT REORGANIZE STANDBY(default)
		//	_objCSM.state = CountryStateMachine.ATTACK;
		//	_objCSM.state = CountryStateMachine.DEFEND;
		//	_objCSM.state = CountryStateMachine.PUT;
		//	_objCSM.state = CountryStateMachine.REORGANIZE;
		//	_objCSM.state = CountryStateMachine.STANDBY;
		}
		

//_________________________________________________________________________________ Public Methods

		/**
		 * Inicializa la clase
		 */
		public function init(parent:DisplayObject = null):void
		{
			mTrace("#Country " + _objCSM.state + "# init");
			_PARENT = parent || _PARENT;
		
			_MC = _objData.movie;
			_PARENT.addChild(_MC);
			_MC.x = _objData.x;
			_MC.y = _objData.y;
			
			var colorTransform:ColorTransform = _MC.transform.colorTransform;
			colorTransform.color = _objData.color;
			_MC.transform.colorTransform = colorTransform;
			_MC.alpha = .2;

			_PARENT.getChildAt(3).getChildByName("c" + _objData.id).mcColor.transform.colorTransform = colorTransform;
			_PARENT.getChildAt(3).getChildByName("c" + _objData.id).mcColor.alpha = 0.8;
			_PARENT.getChildAt(3).getChildByName("c" + _objData.id).soldiers.text = _objData.id;
			if(_objData.player == "NEGRO" || _objData.player == "ROJO" || _objData.player == "AZUL"){
				var white:TextFormat = new TextFormat( );
				white.color = 0xFFFFFF;
				_PARENT.getChildAt(3).getChildByName("c" + _objData.id).soldiers.setTextFormat(white);				
			}
			

/*
			_MC.addEventListener(MouseEvent.MOUSE_OVER,	onMouseOver);
			_MC.addEventListener(MouseEvent.MOUSE_OUT,	onMouseOut);
			_MC.addEventListener(MouseEvent.MOUSE_DOWN,	onMouseDown_);
			_MC.addEventListener(MouseEvent.MOUSE_UP,	onMouseUp_);
*/			
			_MC.addEventListener(MouseEvent.MOUSE_OVER,	onMouseDown_);
			_MC.addEventListener(MouseEvent.MOUSE_OUT,	onMouseUp_);
		//	_MC.addEventListener(MouseEvent.MOUSE_DOWN,	onMouseDown_);
		//	_MC.addEventListener(MouseEvent.MOUSE_UP,	onMouseUp_);
		
		
		//	trace(_PARENT.getChildByName("LimitsCountries"))
		//	trace(_PARENT.getChilds())
			
		}





		/**
		 * Destruye las instancias
		 */
		public function destroy():void
		{
			mTrace("#Country " + _objCSM.state + "# destroy");
		}
		
		
		/**
		 * Congela / descongela los eventos de la clase
		 * @param   freeze 
		 */		
		public function freeze(freeze:Boolean):void
		{
			mTrace("#Country " + _objCSM.state + "# freeze");
			_isFreeze = freeze;
			if(_isFreeze)	_MC.alpha = .7;
			else			_MC.alpha = .2;
		}
		

//_________________________________________________________________________________ Private Methods

		private function onMouseOver(e:MouseEvent):void
		{
			_MC.alpha = .9;
		}
		
		
		private function onMouseOut(e:MouseEvent):void
		{
			_MC.alpha = .2;
		}

		private function onMouseDown_(e:MouseEvent):void
		{
			_MC.alpha = 1;
			dispatchEvent(new CountryEvent(CountryEvent.EVENT, {func:"limitrofes", param:{visible:true, country:_objData.index}}));
			trace(_objData.id+"."+_objData.index);
		}
		
		
		private function onMouseUp_(e:MouseEvent):void
		{
			_MC.alpha = .2;
			dispatchEvent(new CountryEvent(CountryEvent.EVENT, {func:"limitrofes", param:{visible:false, country:_objData.index}}));
		}


//_________________________________________________________________________________ GETTERS

		public function get cData()	:Object	{ return _objData; }

//_________________________________________________________________________________ SETTERS	

		public function set cData	(p:Object)	{ _objData = p; }

	}
}